package ar.edu.unq.wannaplay.pages.abm.employees;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.components.WPLink;
import ar.edu.unq.wannaplay.dtos.EmployeeDTO;
import ar.edu.unq.wannaplay.dtos.LicenceDTO;
import ar.edu.unq.wannaplay.dtos.ProjectDTO;
import ar.edu.unq.wannaplay.services.EmployeeService;

public class EmployeeRegisterPage extends WebPage {
    private static final long serialVersionUID = 1L;

    @SpringBean(name = "service.employeeService")
    private EmployeeService employeeService;

    private EmployeeDTO employee;

    private Panel responsePage;

    public EmployeeRegisterPage(final PageParameters parameter) {
        this.employee = this.getEmployeeService().getEmployeeDTOById(parameter.getString("employee"));
        this.addProjectsTable();
        this.addLicenceTable();
    }

    private void addLicenceTable() {
        this.add(new PageableListView<LicenceDTO>("licenceTable", this.getEmployee().getRegister().getLicences(), 5) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<LicenceDTO> item) {
                LicenceDTO licence = item.getModelObject();
                item.add(new Label("licenceName", licence.getName()));
                item.add(new Label("licenceStart", licence.getInterval().getStart().toString()));
                item.add(new Label("licenceEnd", licence.getInterval().getEnd().toString()));
            }
        });
        PageParameters parameter = new PageParameters();
        parameter.add("employee", this.getEmployee().getDni());
        this.add(new WPLink<AddLicencePage>("addLicence", AddLicencePage.class, parameter));
    }

    private void addProjectsTable() {
        this.add(new PageableListView<ProjectDTO>("projectTable", this.getEmployee().getRegister().getProjects(), 5) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<ProjectDTO> item) {
                ProjectDTO project = item.getModelObject();
                item.add(new Label("projectName", project.getName()));
                item.add(new Label("projectClient", project.getClient().getName()));
            }

        });
    }

    // setters&getters
    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public EmployeeDTO getEmployee() {
        return employee;
    }

    public void setEmployee(final EmployeeDTO employee) {
        this.employee = employee;
    }

    public void setResponsePage(final Panel responsePage) {
        this.responsePage = responsePage;
    }

    public Panel getResponsePage() {
        return responsePage;
    }
}
